package org.hhchat.leetcode.bit;

import java.util.Stack;

/**
 * https://leetcode.com/problems/reverse-bits/description/
 */

public class code190_easy {

    static public class Solution {
        // you need treat n as an unsigned value
        public int reverseBits(int n) {
            Stack<Integer> bin = new Stack<>();
            if(n==0){
                return 0;
            }
            while(n>0){
                bin.push(n%2);
                n/=2;
            }
            while (bin.size()<32){
                bin.push(0);
            }
            long res=0;
            int size = bin.size();
            for(int i=0;i<size;i++) {
                res+=Math.pow(2,i)*bin.pop();
            }
            return (int) res;
        }
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.reverseBits(2147483647));
    }
}
